//
//  SDCache.h
//  SDCache
//
//  Created by yoxnet on 15/11/23.
//  Copyright © 2015年 yoxnet. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
/*
 *   缓存管理类，主要存储字典，数组，字符串，数据
 *   适合中型数据存储
 *
 *****/

/**存储数据或者读取数据的模式*/
typedef NS_ENUM(NSInteger,SDCacheType){
    SDCacheTypeNone,                                                //不操作
    SDCacheTypeMemory = 0x01,                                       //内存
    SDCacheTypeDisk = 0x10,                                         //磁盘
    SDCacheTypeMemoryAndDisk =SDCacheTypeMemory|SDCacheTypeDisk    //默认
    
};

@interface SDCache <KeyType,ObjectType>: NSObject


/**缓存的最长时间,默认是一周，单位:秒*/
@property(nonatomic,assign) NSUInteger cacheMaxAge ;



/**缓存类单例*/
+(instancetype)shareSDCacheManager;


/**释放单例对象，最好别用*/
+(void)releaseShareSDCacheManager;


/**存储NSString,NSArray,NSDictionary,NSData对象*/
-(void)storeObjectSDCache:(id)obj forkey:(NSString *)key;

/**存储对象，不能有自定义对象，key做索引，type存储类型*/
-(void)storeObjectSDCache:(id)obj forKey:(NSString *)key options:(SDCacheType)type;


/**清空所有缓存*/
- (void)clearSDCacheOnCompletion:(void(^)(void))completion  options:(SDCacheType)type;

/**清空磁盘缓存*/
- (void)clearSDCacheDiskOnCompletion:(void(^)(void))completion;

/********
 *
 *  从磁盘读取key对应的文件，如果不存在，返回空
 *  如果存在，是json数据，会返回相应的数组或者字典
 *  否则，返回字符串，不能转成字符串的，返回NSData
 *
 *********/

/**通过key去缓存路径查找，先从内存查找，如果没找到再从磁盘查找*/
-(id)objectInCacheForKey:(NSString *)key;

/**isData设置YES，直接返回NSData文件数据 只在SDCacheTypeDisk SDCacheTypeMemoryAndDisk有效*/
-(id)objectInCacheForKey:(NSString *)key isData:(BOOL)isData;

/**查找key对应的缓存从指定参数类型的缓存 */
-(id)objectInCacheForKey:(NSString *)key options:(SDCacheType)type isData:(BOOL)isData;

/**删除key对应的缓存，先删除内存，再删除磁盘*/
-(void) removeItemInCacheForKey:(NSString *)key;

-(void) removeItemInCacheForKey:(NSString *)key onCompletion:(void(^)(NSError * error))completion;

/**删除key对应的缓存,删除失败时block会返回错误信息,block在删除磁盘时可用*/
-(void) removeItemInCacheForKey:(NSString *)key
                        options:(SDCacheType)type
                   onCompletion:(void(^)(NSError * error))completion;

/**磁盘中缓存目录下文件占用总大小*/
- (NSUInteger)getSDCacheSize;

/**磁盘中缓存目录下文件总数*/
- (NSUInteger)getSDCacheDiskCount;

/**计算缓存的数据总数和总大小，完成用一个block回调,数据更加精确*/
- (void)calculateSDCacheSizeWithCompletionBlock:(void(^)(NSUInteger fileCount,NSUInteger totalSize))completionBlock;


/**缓存自定义对象 属性都为基本数据类型*/
-(void)storeCustomObjectSDCache:(id)obj forkey:(NSString *)key;

-(void)storeCustomObjectSDCache:(id)obj forkey:(NSString *)key options:(SDCacheType)type;

/**查找自定义对象*/
-(id)customObjectInCacheForKey:(NSString *)key;

-(id)customObjectInCacheForKey:(NSString *)key options:(SDCacheType)type;


/**归档对象到磁盘*/
-(void)archiveObjectSDCache:(id)obj forKey:(NSString *)key;

/**解归档对象*/
-(id)unarchiveObjectFromCacheForKey:(NSString *)key;




@end
